package com.durian.finance.controller;

import com.durian.common.core.utils.poi.ExcelUtil;
import com.durian.common.core.web.controller.BaseController;
import com.durian.common.core.web.domain.AjaxResult;
import com.durian.common.core.web.page.TableDataInfo;
import com.durian.common.log.annotation.Log;
import com.durian.common.log.enums.BusinessType;
import com.durian.common.security.annotation.RequiresPermissions;
import com.durian.finance.service.IBankService;
import com.durian.financial.api.domain.Bank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 银行代码Controller
 * 
 * @author durian
 * @date 2022-06-26
 */
@RestController
@RequestMapping("/bank")
public class BankController extends BaseController
{
    @Autowired
    private IBankService bankService;

    /**
     * 查询银行代码列表
     */
    @RequiresPermissions("finance:bank:list")
    @GetMapping("/list")
    public TableDataInfo list(Bank bank)
    {
        startPage();
        List<Bank> list = bankService.selectBankList(bank);
        return getDataTable(list);
    }

    /**
     * 获取所有银行组成的Map
     */
    @RequiresPermissions("finance:bank:list")
    @GetMapping("/bunkMap")
    public AjaxResult getBunkMap(){

        List<Bank> list = bankService.selectBankList(new Bank());
        Map<Long, String> bankMap = list.stream().collect(Collectors.toMap(Bank::getId, Bank::getBankName));
        return AjaxResult.success(bankMap);
    }

    /**
     * 导出银行代码列表
     */
    @RequiresPermissions("finance:bank:export")
    @Log(title = "银行代码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bank bank)
    {
        List<Bank> list = bankService.selectBankList(bank);
        ExcelUtil<Bank> util = new ExcelUtil<Bank>(Bank.class);
        util.exportExcel(response, list, "银行代码数据");
    }

    /**
     * 获取银行代码详细信息
     */
    @RequiresPermissions("finance:bank:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bankService.selectBankById(id));
    }

    /**
     * 新增银行代码
     */
    @RequiresPermissions("finance:bank:add")
    @Log(title = "银行代码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bank bank)
    {
        return toAjax(bankService.insertBank(bank));
    }

    /**
     * 修改银行代码
     */
    @RequiresPermissions("finance:bank:edit")
    @Log(title = "银行代码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bank bank)
    {
        return toAjax(bankService.updateBank(bank));
    }

    /**
     * 删除银行代码
     */
    @RequiresPermissions("finance:bank:remove")
    @Log(title = "银行代码", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bankService.deleteBankByIds(ids));
    }
}
